package org.hackystat.devcathlon.engine.commentary;

import java.io.Serializable;
import java.text.DateFormat;
import java.util.Locale;

import javax.xml.datatype.XMLGregorianCalendar;
import org.hackystat.devcathlon.engine.user.User;
import org.hackystat.utilities.tstamp.Tstamp;

/**
 * Provides information about a single comment.
 * Comments are managed (created, retrieved, etc.) via the CommentManager. 
 * @author John Zhou
 */
public class Comment implements Serializable {
  /**
   * 
   */
  private static final long serialVersionUID = 1L;
  
  /** The author of the comment. */
  private User user;
  /** The date of the comment. */
  private String date;
  /** The contents of the comment. */
  private String comment;
  
  /**
   * Creates a new comment. Typically done by the CommentManager.  
   * @param user The user. 
   * @param date The date. 
   * @param comment The comment. 
   */
  public Comment(User user, XMLGregorianCalendar date, String comment) {
    this.user = user;
    this.date = date.toString();
    this.comment = comment;
  }

  /**
   * Return the author of the comment.
   * @return user The user.
   */
  public User getUser() {
    return user;
  }

  /**
   * Return the date of the comment.
   * @return date The date.
   * @throws Exception If there is an error with Tstamp
   */
  public XMLGregorianCalendar getTimestamp() throws Exception {
    return Tstamp.makeTimestamp(date);
  }
  
  /**
   * Returns a printable string of the date and time of the comment.
   * 
   * @return The date and time in the format (mm/dd/yy  hh:mm pm/am).
   * @throws Exception If there is an error error with the timestamp.
   */
  public String printDateTime() throws Exception {
    DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT,
        new Locale("en", "US"));
    return formatter.format(this.getTimestamp().toGregorianCalendar().getTime());
  }

  /**
   * Return the contents of the comment.
   * @return comment The comment.
   */
  public String getComment() {
    return comment;
  }
}
